package service

import (
	"fmt"
	"gitee.com/kristas/booting-go/framework"
	"gitee.com/kristas/booting-go/framework/cache"
	"gitee.com/kristas/booting-go/framework/core/statement"
	"gitee.com/kristas/booting-go/framework/core/statement/types"
)

func init() {
	framework.Component(new(DemoCacheServiceImpl))
}

type DemoCacheService interface {
	statement.Service
	SaveCache(key, val string) interface{}
	GetFromCache(key string) interface{}
}

type DemoCacheServiceImpl struct {
	types.Service `service:"DemoCacheService"`
	Cache         cache.ICache `wire:""` //使用cache注入缓存实现
}

func (r *DemoCacheServiceImpl) SaveCache(key, val string) interface{} {
	resp, err := r.Cache.Set(key, val)
	if err != nil {
		return err.Error()
	}
	return map[string]string{
		"msg":   resp,
		"key":   key,
		"value": val,
	}
}

func (r *DemoCacheServiceImpl) GetFromCache(key string) interface{} {
	resp, err := r.Cache.Get(key)
	if err != nil {
		return err.Error()
	}
	return fmt.Sprintf("%s", resp)
}
